package com.jian.web;

import com.jian.dao.impl.UserDaoImpl;
import com.jian.pojo.User;
import com.jian.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author Jiange
 * @create 2021-04-06-21:12
 */
public class RegisterService extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
        System.out.println("init");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        System.out.println(username);
        System.out.println(password);

        UserServiceImpl userService = new UserServiceImpl();


        if(username.isEmpty() || password.isEmpty()){
            resp.sendRedirect("http://localhost:8080/index.html");
            System.out.println("来过了但是放过来的有空东西");
        }else {

            try {
                userService.regiest(new User(username,password,email));
            } catch (SQLException e) {
                resp.sendRedirect("http://localhost:8080/index.html");
                System.out.println("重复出现名称");
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getParameter("username"));
    }
}
